/**
 * 站内信
 */
import api from '../../tools/api'
import {SUCCESS_STATUS} from '../../config/status'
import promise from '../../tools/promise'
import {
  INMAIL_LIST,
  INMAIL_INFO
} from '../mutation-types'

const state = {
  pageData: {
    total: 1,
    per_page: 15,
    current_page: 1,
    last_page: 1
  },
  mailList: [],
  mailInfo: {}
}

const mutations = {
  [INMAIL_LIST] (state, payload) {
    state.mailList = payload.data
    if (payload.data) {
      let len = payload.data.length
      for (let i = 0; i < len; i++) {
        state.mailList[i].orther_info = JSON.parse(payload.data[i].orther_info)
      }
    }
    state.pageData.total = Number(payload.total)
    state.pageData.per_page = Number(payload.per_page)
    state.pageData.current_page = Number(payload.current_page)
    state.pageData.last_page = Number(payload.last_page)
  },
  [INMAIL_INFO] (state, payload) {
    state.mailInfo = payload
  }
}

const actions = {
  // 获取站内信列表
  async mailList ({commit}, payload) {
    let listRes = await api('servermessage/message/Message/adminMessageList', payload)
    commit(INMAIL_LIST, listRes.res)
  },
  // 删除站内信
  async mailDel ({commit}, payload) {
    let delRes = await api('servermessage/message/Message/adminDelOneMessage', payload)
    return promise(delRes)
  },
  // 获取站内信详情
  async mailInfo ({commit}, payload) {
    let infoRes = await api('servermessage/message/Message/adminMessageDetail', payload)
    if (SUCCESS_STATUS === infoRes.status) {
      commit(INMAIL_INFO, infoRes.res)
    }
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
